package ui.home.column3.notice

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import model.enums.ApplyState
import model.enums.NoticeType
import model.vo.FriendNotificationItem
import theme.EasyChatTheme
import ui.compose.NetworkImage
import ui.home.HomeViewModel


/**
 * @author wei kunJie
 * @date 2024/4/14 21:50:28
 */
@Composable
fun FriendNotification(easyChatTheme: MutableState<EasyChatTheme>,modifier: Modifier, items: MutableList<FriendNotificationItem>, homeViewModel: HomeViewModel) {
    Column(modifier = modifier.background(color = easyChatTheme.value.mainColor2)) {
        Spacer(modifier = Modifier.height(33.dp))
        //标题行
        Row(modifier = Modifier.fillMaxWidth().padding(horizontal = 25.dp)) {
            Text(text = "好友通知", modifier = Modifier.weight(1f), fontSize = 18.sp)
            IconButton(modifier = modifier.size(20.dp),
                onClick = {}) {
                Icon(
                    modifier = modifier.fillMaxSize(),
                    painter = painterResource("icon/ic_delete.svg"),
                    contentDescription = "删除全部"
                )
            }
        }
        Spacer(modifier = Modifier.height(25.dp))
        LazyColumn(modifier = Modifier.weight(1f).fillMaxWidth()) {
            items(items) { item ->
                FriendNotificationRow(easyChatTheme,item, homeViewModel)
                Spacer(modifier = Modifier.height(15.dp))
            }
        }
    }
}

@Composable
fun FriendNotificationRow(easyChatTheme: MutableState<EasyChatTheme>, item: FriendNotificationItem, homeViewModel: HomeViewModel) {
    Box(
        modifier = Modifier.padding(horizontal = 50.dp).background(
            color = easyChatTheme.value.mainWhite,
            shape = RoundedCornerShape(5.dp)
        )
    ) {
        Row(modifier = Modifier.padding(15.dp), verticalAlignment = Alignment.CenterVertically) {
            //头像
            NetworkImage(
                url = item.headImage,
                painterFor = { remember { BitmapPainter(it) } },
                contentDescription = "头像",
                modifier = Modifier.size(50.dp).clip(CircleShape),
                contentScale = ContentScale.Crop,
                defaultPainterFor = painterResource("image/default_head.png")
            )
            Spacer(modifier = Modifier.width(15.dp))
            Column {
                Row {
                    Text(text = item.userName, fontSize = 14.sp, color = easyChatTheme.value.mainColor)
                    Spacer(modifier = Modifier.width(4.dp))
                    if (item.noticeType == NoticeType.friendApply)
                        Text(text = "请求添加为好友", fontSize = 14.sp)
                    else if (item.noticeType == NoticeType.friendApplyProcessed)
                        Text(text = "处理了你对Ta的好友申请", fontSize = 14.sp)
                    Spacer(modifier = Modifier.width(4.dp))
                    Text(text = item.time, fontSize = 14.sp, color = Color(0XEE696969))
                }
                Spacer(modifier = Modifier.height(10.dp))
                Text(text = item.content, fontSize = 14.sp, color = Color(0XEE696969))
            }
            Spacer(modifier = Modifier.weight(1f))
            if (item.applyState.equals(ApplyState.WAITING)) {
                Button(
                    onClick = {
                        item.id?.let { homeViewModel.processFriendRequest(ApplyState.PASSED, it, item.friendAccount) }
                    },
                    colors = ButtonDefaults.buttonColors(
                        backgroundColor = easyChatTheme.value.mainColor,
                    )
                ) {
                    Text(text = "同意", color = Color.White)
                }
                Spacer(modifier = Modifier.width(12.dp))
                Button(
                    onClick = {
                        item.id?.let { homeViewModel.processFriendRequest(ApplyState.PASSED, it, item.friendAccount) }
                    },
                    colors = ButtonDefaults.buttonColors(
                        backgroundColor = Color(0XFFC75450)
                    )
                ) {
                    Text(text = "拒绝", color = Color.White)
                }
            } else if (item.applyState.equals((ApplyState.PASSED))) {
                Text(text = "已同意", color = Color(0XEE696969))
            } else if (item.applyState.equals((ApplyState.REJECTED))) {
                Text(text = "已拒绝", color = Color(0XEE696969))
            }
        }
    }
}